package cn.itsource.pethome.pet.service.impl;

import cn.itsource.pethome.base.service.impl.BaseServiceImpl;
import cn.itsource.pethome.order.domain.OrderPetAcquisition;
import cn.itsource.pethome.order.mapper.OrderPetAcquisitionMapper;
import cn.itsource.pethome.org.domain.Shop;
import cn.itsource.pethome.org.mapper.ShopMapper;
import cn.itsource.pethome.pet.domain.Pet;
import cn.itsource.pethome.pet.domain.SearchMasterMsg;
import cn.itsource.pethome.pet.domain.SearchMasterMsgAuditLog;
import cn.itsource.pethome.pet.domain.dto.MasterMsgDTO;
import cn.itsource.pethome.pet.mapper.PetMapper;
import cn.itsource.pethome.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.itsource.pethome.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pethome.pet.service.ISearchMasterMsgService;
import cn.itsource.pethome.user.domain.User;
import cn.itsource.pethome.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zcj
 * @since 2022-10-26
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Override
    @Transactional
    public void insert(SearchMasterMsg searchMasterMsg, User user) {
        //做什么事情
        //校验 前端请求的参数
        if(StringUtils.isEmpty(searchMasterMsg.getAddress())){
            throw new BusinessException("请输入地址");
        }
        //审核  自动审核
        String text =  searchMasterMsg.getTitle()
                +"|"+ searchMasterMsg.getName()
                +"|"+ searchMasterMsg.getGender()
                +"|"+ searchMasterMsg.getAddress();

        SearchMasterMsgAuditLog log = new SearchMasterMsgAuditLog();

        //审核失败？
        if(!BaiduAuditUtils.baiduTextVerify(text)){
            //抛出异常 告诉用户重写填写
            throw new BusinessException("内容不合法，请求重新填写");
        }
        //审核成功？
          //当前用户给的地址周围50公里是否有商家 
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        List<Shop> shops = shopMapper.queryAll();
        //返回是最近的一个店铺
        Shop shop = DistanceUtil.getNearestShop(point, shops);

        searchMasterMsg.setState(1);
        searchMasterMsg.setNote("审核通过");
        //没有
        if(shop != null){
            //有
            searchMasterMsg.setShopId(shop.getId());
            System.out.println("你来生意了！赶快去接客！"+searchMasterMsg.getTitle());
            //要去提示商家
        }
          //进行保存和日志的记录
        searchMasterMsg.setUserId(user.getId());//当前发布的人的id保存进去
        super.insert(searchMasterMsg);
        log.setMsgId(searchMasterMsg.getId());
        log.setAuditTime(new Date());
        log.setNote("审核通过");
        log.setState(1);
        searchMasterMsgAuditLogMapper.insert(log);
    }

    @Override
    public void accept(MasterMsgDTO dto) {
        Long msgId = dto.getMsgId();//寻主的消息
        //做什么事情
        OrderPetAcquisition order = orderPetAcquisitionMapper.queryByMsgId(msgId);
        if(order!=null){
            throw new BusinessException("请不要重复接单");
        }
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.queryOne(msgId);
        Pet pet = gotoPet(searchMasterMsg);
        //生成一个订单
        gotoOrder(dto, searchMasterMsg, pet);
        //没有了
        //发送邮件通知人去收狗
    }

    @Override
    public void reject(MasterMsgDTO dto) {
        Long msgId = dto.getMsgId();//寻主的消息
        //做什么事情
        OrderPetAcquisition order = orderPetAcquisitionMapper.queryByMsgId(msgId);
        if(order!=null){
            throw new BusinessException("已接单！不能拒");
        }
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.queryOne(msgId);
        searchMasterMsg.setShopId(null);
        searchMasterMsgMapper.update(searchMasterMsg);
    }

    private void gotoOrder(MasterMsgDTO dto, SearchMasterMsg searchMasterMsg, Pet pet) {
        OrderPetAcquisition orderPetAcquisition = new OrderPetAcquisition();
        orderPetAcquisition.setOrderSn(CodeGenerateUtils.generateOrderSn(pet.getId()));
        orderPetAcquisition.setDigest(dto.getNote());
        //获取当前时间
        Long lastTime = System.currentTimeMillis()+(30*1000*60);//定时任务自动去扫描
        orderPetAcquisition.setLastcomfirmtime(new Date(lastTime));
        orderPetAcquisition.setState(1);
        //和钱相关的一定要用BigDecimal
        BigDecimal bigDecimal = new BigDecimal(searchMasterMsg.getPrice());
        orderPetAcquisition.setPrice(bigDecimal);
        orderPetAcquisition.setPetId(pet.getId());
        orderPetAcquisition.setUserId(searchMasterMsg.getUserId());
        orderPetAcquisition.setShopId(searchMasterMsg.getShopId());
        orderPetAcquisition.setAddress(searchMasterMsg.getAddress());
        orderPetAcquisition.setEmpId(dto.getHandler());//处理人的ID
        orderPetAcquisition.setSearchMasterMsgId(searchMasterMsg.getId());
        orderPetAcquisitionMapper.insert(orderPetAcquisition);
    }

    private Pet gotoPet(SearchMasterMsg searchMasterMsg) {
        //同步宠物信息
        Pet pet = new Pet();
        pet.setName(searchMasterMsg.getName());
        pet.setCostprice(searchMasterMsg.getPrice().toString());
        pet.setShopId(searchMasterMsg.getShopId());
        pet.setTypeId(searchMasterMsg.getPetType());
        pet.setSearchMasterMsgId(searchMasterMsg.getId());
        petMapper.insert(pet);
        return pet;
    }
}
